package ooday04;

public class PolymorphicDemo {
    public static void main(String[] args) {
        /*//多态,向上造型
        Dog o1=new Dog("小黑",2,"黑");
        //o1.name/age/color/eat()/drink()/lookHome()/swim()
        Animal o2=new Dog("雷欧",2,"黑");
        //02.name/age/color/eat()/drink()
        Swim o3=new Dog("小黑",2,"黑");
        o3.swim();

        Chick o4=new Chick("小黄",1,"黄");
        Fish o5 =new Fish("泡泡",1,"黄");
        System.out.println("_________________________");
        //重写被调看对象     .看引用的类型
        Master master=new Master();
        master.feed(o1);//在传参的同时，系统会自动进行向上造型
        master.feed(o2);
        master.feed(o4);
        master.feed(o5);
        System.out.println("_________________________");*/
        //多态数组
        Animal[] animals=new Animal[4];
        animals[0]=new Dog("小黑",2,"黑");
        animals[1]=new Dog("雷欧",2,"黑");
        animals[2]=new Chick("小黄",1,"黄");
        animals[3]=new Fish("泡泡",1,"黄");
        for(int i=0;i<animals.length;i++){
            System.out.println(animals[i].name);
            animals[i].drink();//每个动物喝水
            animals[i].eat();
            //不同动物调用不同的特有方法
            //向下强转类型
            if (animals[i] instanceof Dog){
                Dog dog=(Dog) animals[i];
                dog.lookHome();
            }
            if (animals[i] instanceof Chick){
                Chick chick=(Chick) animals[i];
                chick.layEggs();
            }
            if (animals[i] instanceof Swim){//适用于实现Swim接口的
                Swim swim=(Swim) animals[i];
                swim.swim();
            }
            //能用父绝不用子类
        }
        /*//演示强制转换成功的条件
        Animal o=new Dog("小黑o",2,"黑");
        Dog d=(Dog) o;
        Swim s=(Swim) o;
        //Fish f=(Fish) o;//编译错误会发生classcastException
        System.out.println(o instanceof Dog);
        System.out.println(o instanceof Swim);
        System.out.println(o instanceof Animal);
        System.out.println(o instanceof Fish);
        System.out.println("_________________________");
        if (o instanceof Fish){
            Fish f1=(Fish) o;
        }else {
            System.out.println("o不是Fish 类型的");
        }*/

    }
}
